package com.beeweeb.services.tps.util;

import java.util.ArrayList;
import java.util.Collection;

/**
 * An ArrayList with a maximum capacity
 * 
 * @author guido d'albore
 */
public class BoundedArrayList<E> extends ArrayList<E> {
    int maximumCapacity;
    
    public BoundedArrayList(int maximumCapacity) {
        this.maximumCapacity = maximumCapacity;
    }

    @Override
    public boolean add(E e) {
        if(size() < maximumCapacity) {
            return super.add(e);
        }
        return false;
    }

    @Override
    public void add(int index, E element) {
        if((index >= 0) && (index <= maximumCapacity) && (size() < maximumCapacity)) {
            super.add(index, element);
        }
    }

    @Override
    public boolean addAll(Collection<? extends E> c) {
        if((size() + c.size()) <= maximumCapacity) {
            return super.addAll(c);
        }
        return false;
    }

    @Override
    public boolean addAll(int index, Collection<? extends E> c) {
        if(((index + c.size()) <= maximumCapacity) && ((size() + c.size()) <= maximumCapacity)) {
            super.addAll(index, c);
        }

        return false;
    }     
}
